package org.geektimes.configuration.micro.config.source.servlet;

import org.geektimes.configuration.micro.config.source.MapBasedConfigSource;

import javax.servlet.ServletContext;
import java.util.Enumeration;
import java.util.Map;

public class ServletConfigSource extends MapBasedConfigSource {
//    private static ServletContext servletContext;
    private ServletContext servletContext;

//    public ServletConfigSource() {
//        super("ServletConfigSource", 500);
//    }

//    public static ServletConfigSource getInstance(ServletContext servletContext){
//        ServletConfigSource.servletContext = servletContext;
//        return new ServletConfigSource();
//    }

    public ServletConfigSource(ServletContext servletContext) {
        super("ServletConfigSource", 500);
        this.servletContext = servletContext;
    }

    @Override
    protected void prepareConfigData(Map<String, String> configData) throws Throwable {
        Enumeration<String> initParameterNames = servletContext.getInitParameterNames();
        while (initParameterNames.hasMoreElements()){
            String parameterName = initParameterNames.nextElement();
            configData.put(parameterName,servletContext.getInitParameter(parameterName));
        }
    }
}
